package com.anytrek.cloudcommon.config;

import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.anytrek.cloudcommon.utils.ApiResult;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

/**
 * @ ClassName OrderFeignFallbackFactory
 * @ Author Alex2
 * @ Date 2025/8/27 17:38
 **/
@Component
@Log4j2
public class GlobalBlockExceptionHandler  implements BlockExceptionHandler {

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s, BlockException e) throws Exception {
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        ApiResult result = ApiResult.error("接口访问过于频繁，请稍后再试");
        httpServletResponse.getWriter().write(objectMapper.writeValueAsString(result));
    }
}